package com.lovo.frame;

import com.lovo.pojo.UsersDO;
import com.lovo.service.UserService;
import com.lovo.service.impl.UserServiceImpl;
import com.lovo.util.DBUtil;
import com.lovo.util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AlterPassword extends JPanel implements ActionListener {
    private Icon sureBtnImg = Util.getIcon("/image/buttonSure.jpg");
    private JPasswordField jPasswordField;
    private JPasswordField jPasswordField1;
    private JButton sure;
    private ATM frame;
    private UserService userService = new UserServiceImpl();

    public AlterPassword(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        JLabel textlabel1 = new JLabel("旧密码：");
        textlabel1.setBounds(200, 150, 50, 30);
        this.add(textlabel1);
        //旧密码
        this.setOpaque(false);
        jPasswordField = new JPasswordField();
        jPasswordField.setBounds(250, 150, 250, 30);
        this.add(jPasswordField);

        JLabel textlabel2 = new JLabel("新密码：");
        textlabel2.setBounds(200, 200, 50, 30);
        this.add(textlabel2);
        //新密码
        this.setOpaque(false);
        jPasswordField1 = new JPasswordField();
        jPasswordField1.setBounds(250, 200, 250, 30);
        this.add(jPasswordField1);


        this.setOpaque(false);
        sure = new JButton(sureBtnImg);
        sure.setBounds(200, 320, 100, 20);
        this.add(sure);
        sure.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sure) {
            String oldPwd = String.valueOf(jPasswordField.getPassword());
            String newPwd = String.valueOf(jPasswordField1.getPassword());
            try {
                userService.changePwd(oldPwd, newPwd, frame.getLoginUser().getId());
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this, exception.getMessage());
            }
            frame.switchPanel("登陆");
        }
    }
}
